<template>
	<view>
		<view>
			<view class="header">
				<!-- <image src="/static/icons/wx_login.png"></image> -->
			</view>
			<view class="content">
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像等)</text>
			</view>
			<button class="bottom" @click="wxLogin()">授权登录</button>
		</view>
		<!-- <view wx:else>请升级微信版本</view> -->
		<div class="protocol">
			<u-checkbox shape="circle" v-model="checked" :disabled="false"></u-checkbox>
			<div class="text">
				{{ $t('我已同意并愿意遵守') }}
				<text @click="goPage('用户协议', '1')">《{{ $t('用户协议') }}》</text>
				、
				<text @click="goPage('隐私协议', '5')">《{{ $t('隐私协议') }}》</text>
			</div>
		</div>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import { mapState } from 'vuex';
import $config from '@/api/http.baseUrl.js';
export default {
	data() {
		return {
			scene: '',
			check_switch: false,
			checked: false,
		};
	},
	onLoad(options) {
		this.scene = options.scene;
		console.log(this.baseUrl);
		this.getConf();
	},
	methods: {
		goPage(name, id) {
			this.$u.route('/pages/login/protocol', {
				name,
				id
			});
		},
		getConf() {
			this.$u.api.conf().then(res => {
				this.check_switch = res.data.check_switch;
				console.log(this.check_switch);
			});
		},
		wxLogin() {
			if (this.checked === false) {
				this.$u.toast('请勾选用户协议');
				return;
			}
			let loginCode;
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					loginCode = loginRes.code;
				}
			});
			uni.getUserProfile({
				desc: '用于完善用户资料',
				success: getUserRes => {
					let data = {
						wx_code: loginCode,
						iv: getUserRes.iv,
						rawData: getUserRes.rawData,
						signature: getUserRes.signature,
						encryptedData: getUserRes.encryptedData,
						regcode: this.scene,
						type: 1
					};
					uni.request({
						url: `${$config.baseUrl}/member/login/auth`,
						data: data,
						method: 'POST',
						success: res => {
							console.log('res: ', res);
							if (res.data.code === 0) {
								uni.$u.toast(res.data.msg);
								return;
							}

							if (res.data.code === 2) {
								uni.redirectTo({
									url: `/pages/login/bindingPhone?id=${res.data.data.openid}`
								});
							}
							if (res.data.code === 1) {
								uni.setStorageSync('token', res.data.data.token.access_token);
								// uni.setStorageSync('userInfo', res.data.data.user);
								this.$store.dispatch('websocketInit')
								uni.switchTab({
									url: '/pages/mine/index'
								});
							}

						}
					});
				},
				fail: err => {
					console.log(err);
				}
			});
			// wechatLogin(this.scene);
		}
	}
};
</script>

<style lang="scss" scoped>
.header {
	margin: 90rpx 0 90rpx 50rpx;
	border-bottom: 1px solid #ccc;
	text-align: center;
	width: 650rpx;
	height: 100rpx;
}

.header image {
	width: 200rpx;
	height: 200rpx;
}

.content {
	margin-left: 50rpx;
	margin-bottom: 90rpx;
}

.content text {
	display: block;
	color: #9d9d9d;
	margin-top: 40rpx;
}

.bottom {
	border-radius: 80rpx;
	margin: 70rpx 50rpx;
	font-size: 35rpx;
	background-color: #1aad19;
	color: #fff;
	border: none;
	outline: none;
}
.protocol {
	display: flex;
	position: fixed;
	bottom: 100rpx;
	color: #999999;
	padding: 20rpx;

	.text {
		flex: 1;
		font-size: 26rpx;
	}

	text {
		color: #009dff;
	}
}
</style>
